package com.efounder.frame.activity.manager;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.core.xml.StubObject;
import com.efounder.frame.fragment.EFAppAccountPagerSlidingTabFragment;
import com.efounder.frame.tabbar.EFTabBar;
import com.efounder.mobilemanager.R;

import java.util.List;

/**
 * Created by XinQing on 2016/8/23.
 * EFAppAccountMainActivity Fragment 管理类
 */
public class EFAppAccountMainActivityFragmentManager {
    /** fragment tag 前缀 **/
    public static final String FRAGMENT_TAG_PREFIX = "EFMainActivity_fragment_tag_";
    /**此position需要做回被收后，恢复数据的处理:onSaveInstanceState**/
    private int currentPosition = -1;

    private FragmentManager fragmentManager;
    /**管理EFTabBar，控制其显示位置（管理EFTabBar.setSelectedTab）**/
    private EFTabBar efTabBar;

    public EFAppAccountMainActivityFragmentManager(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    /**
     * 改变fragment：隐藏当前fragment，显示目标fragment
     * @param targetPosition 目标位置
     * @param isCacheFragment 是否缓存fragment
     * @return targetPosition
     */
    public EFAppAccountPagerSlidingTabFragment showFragment(int targetPosition, StubObject targetData, boolean isCacheFragment){
        if (currentPosition == targetPosition){
            return (EFAppAccountPagerSlidingTabFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + currentPosition);
        }
        EFAppAccountPagerSlidingTabFragment targetFragment = null;
//        Fragment testFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + targetPosition);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (isCacheFragment){
            //1.显示某个位置的fragment
            targetFragment = showFragmentRaw(fragmentTransaction,targetPosition,targetData);
            //2.如果上个位置有fragment，隐藏掉
            Fragment currentFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + currentPosition);
            if (currentFragment != null && !currentFragment.isHidden()) {// 2.2隐藏当前显示的Fragment
                fragmentTransaction.hide(currentFragment);
            }
        }else {
            //如果不缓存，直接replace
            targetFragment = EFAppAccountPagerSlidingTabFragment.newInstance(targetPosition,targetData);
            fragmentTransaction.replace(R.id.fragment_container,targetFragment,FRAGMENT_TAG_PREFIX + targetPosition);
        }

        fragmentTransaction.commit();
//        testFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + currentPosition);
        //最后，记录position
        currentPosition = targetPosition;
        efTabBar.setSelectedTab(currentPosition);
        return targetFragment;
    }

    /**
     * raw方法
     * @param fragmentTransaction
     * @param position
     */
    private EFAppAccountPagerSlidingTabFragment showFragmentRaw(FragmentTransaction fragmentTransaction, int position, StubObject data){
        //1.显示某个位置的fragment
        EFAppAccountPagerSlidingTabFragment willShowFragment = (EFAppAccountPagerSlidingTabFragment) fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + position);
        if (willShowFragment == null) {// 如果没有实例化Fragment，则创建
            willShowFragment = EFAppAccountPagerSlidingTabFragment.newInstance(position,data);
            fragmentTransaction.add(R.id.fragment_container,willShowFragment,FRAGMENT_TAG_PREFIX + position);
        } else if(willShowFragment.isHidden()){// 如果已经实例化，则显示
            fragmentTransaction.show(willShowFragment);
        }
        return  willShowFragment;
    }

    /****
     * 同时初始化所有Tab对应的fragment
     * @param dataList
     * @param showFragmentPosition
     */
    public void initAllTabFragment(List<StubObject> dataList, int showFragmentPosition){
//        fragmentManager.executePendingTransactions();//没起作用。。。。
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        for (int i=0; i < dataList.size(); i++){
            Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + i);
            if (fragment == null) {// 如果没有实例化Fragment，则创建
                fragment = EFAppAccountPagerSlidingTabFragment.newInstance(i,dataList.get(i));
                fragmentTransaction.add(R.id.fragment_container,fragment,FRAGMENT_TAG_PREFIX + i);
            }
        }
        fragmentTransaction.commit();
        //最后，记录position
        currentPosition = showFragmentPosition;
        efTabBar.setSelectedTab(currentPosition);
    }

    /**
     * 隐藏底部TabBar的摸个位置对应的Fragment
     * @param position
     */
    public void hideFragment(int position){
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction = fragmentManager.beginTransaction();
        Fragment fragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG_PREFIX + position);
        if (fragment != null && !fragment.isHidden()) {
            fragmentTransaction.hide(fragment);
        }
        fragmentTransaction.commit();
    }

    public int getCurrentPosition() {
        return currentPosition;
    }

    public void setCurrentPosition(int currentPosition) {
        this.currentPosition = currentPosition;
    }

    public EFTabBar getEfTabBar() {
        return efTabBar;
    }

    public void setEfTabBar(EFTabBar efTabBar) {
        this.efTabBar = efTabBar;
    }
}
